from dataclasses import dataclass
from enum import IntEnum


class DiffType(IntEnum):
    DELETE = 0
    ADD = 1
    MODIFY = 2
    NOFOUND = 3
    NOCHANGE = 4


@dataclass
class DiffA2lData:
    name: str
    type: DiffType
    check = False
    inform: tuple
    color: str = None


def htmltabrowfakelist(myiter, ref=False):
    if myiter:
        temp = '<table>\n'
        tempstr = '<tr><td><a href="#{0}">{0}</a></td></tr>\n' if ref else '<tr><td>{0}</td></tr>\n'
        for x in myiter:
            temp += tempstr.format(x)
        temp += '</table>\n'
    else:
        temp = "无\n"
    return temp

def htmlparag(title, **kwargs):
        return """<p>
<h2><a name="{a[refname]}">{title}</a></h2>
{{0}}
</p>""".format(a=kwargs, title=title)

def htmllist(myiter, ref=False):
    if myiter:
        temp = '<ol>\n'
        tempstr = '<li><a href="#{0}">{0}</a></li>\n' if ref else '<li>{0}</li>\n'
        for x in myiter:
            temp += tempstr.format(x)
        temp += '</ol>\n'
    else:
        temp = "无\n"
    return temp

def htmltabrow(myiter, th=False):
    temp = '<tr>\n'
    tempstr = '<th>{0!s}</th>\n' if th else '<td>{0!s}</td>\n'
    for x in myiter:
        temp += tempstr.format(x)
    temp += '</tr>\n'
    return temp

def gen_tr(key,inform,bgcolor):
    ran = '{0!s}-{1!s}'.format(inform[1], inform[2])
    return ("""
    <tr style="background-color:{bgcolor}">
    <td><a name="{key}"><b>{key}</b></a></td>
    <td>{x[0]}</td>
    <td>{ran}</td>
    <td>{x[3]}</td>
    <td>{x[4]}</td>
    <td>{x[5]}</td>
    </tr>
    <tr style="background-color:{bgcolor}">
    <td colspan="6">{x[6]}</td>
    </tr>
    """.format(key=key, x=inform, ran=ran, bgcolor=bgcolor))


def htmlDiffA2lTab(l:list[DiffA2lData]):
    if not l:return ''
    informlist = []
    informlist.append("""<table border="1" cellpadding="4" cellspacing="0">
    <tr>
        <th>变量名</th>
        <th>单位</th>
        <th>范围</th>
        <th>精度</th>
        <th>地址</th>
        <th>变量类型</th>
    </tr>
    """)
    for i, data in enumerate(l):
        if data.color is not None:
            bgcolor = data.color
        else:
            bgcolor = '#B4EEB4' if i % 2 == 0 else '#F5F5F5'
        if data.inform:
            informlist.append(gen_tr(data.name,data.inform,bgcolor))
        elif data.type == DiffType.DELETE:
            informlist.append(f'<tr style="background-color:{bgcolor}"><td><b>{data.name}</b></td><td colspan="5"><b>已删除</b></td></tr>')
        elif data.type == DiffType.NOFOUND:
            informlist.append(f'<tr style="background-color:{bgcolor}"><td><b>{data.name}</b></td><td colspan="5"><b>没找到</b></td></tr>')
        else:
            assert False,f'data type wrong: {data}'
    informlist.append('</table>\n')
    informstr = ''.join(informlist)
    return informstr


def htmlparInformTab(dic, pars):
    if dic:
        informlist = []
        informlist.append("""<table border="1" cellpadding="4" cellspacing="0">
        <tr>
            <th>变量名</th>
            <th>单位</th>
            <th>范围</th>
            <th>精度</th>
            <th>地址</th>
            <th>变量类型</th>
        </tr>
        """)
        for i, key in enumerate(pars):
            x = dic[key]
            bgcolor = '#B4EEB4' if i % 2 == 0 else '#F5F5F5'
            informlist.append(gen_tr(key,x,bgcolor))
        informlist.append('</table>\n')
        informstr = ''.join(informlist)
    else:
        informstr = ''
    return informstr
